/**
 * 
 * @author  phoenics@126.com
 * @version v.0.1
 * @date  2018年3月14日 下午2:30:38 
 * 
 */
package phoenics.upservice;

import javax.annotation.Resource;
import javax.servlet.MultipartConfigElement;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import phoenics.upservice.auth.AuthError;
import phoenics.upservice.auth.JwtAuthenticationProvider;
import phoenics.upservice.servlet.JwtTokenFilter;

import static org.springframework.http.HttpMethod.POST;  
/**
 *
 */
@EnableWebSecurity  
@Configurable  
public class RestServiceConfiguration extends WebSecurityConfigurerAdapter {
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(RestServiceConfiguration.class);
	 @Resource  
	    private JwtTokenFilter jwtTokenFilter;  
	  
	    @Resource  
	    private JwtAuthenticationProvider jwtAuthenticationProvider;  
	  
	    @Resource  
	    private AuthError authError;  
	  
	    @Override  
	    protected void configure(HttpSecurity http) throws Exception {  
	        http  
	                .cors()  
	                .and()  
	                .csrf()  
	                .disable()  
	                .authorizeRequests()  
	                .antMatchers("/register/*", "/token/*","/payback/*","/v2/api-docs","/configuration/ui", "/swagger-resources/**", "/configuration/security","/swagger-ui.html**", "/webjars/**") // 不拦截
	                .permitAll().antMatchers("/**/*") // 允许拦截  
	                .authenticated()  
	                .and()  
	                .addFilterBefore(jwtTokenFilter, UsernamePasswordAuthenticationFilter.class)  
	                .exceptionHandling()  
	                .authenticationEntryPoint(authError)  
	                .accessDeniedHandler(authError);  
	    }  
	  
	    @Override  
	    protected void configure(AuthenticationManagerBuilder auth) throws Exception {  
	        auth.authenticationProvider(jwtAuthenticationProvider);  
	    }  
	    @Bean
	    public BCryptPasswordEncoder passwordEncoder() {
	        return new BCryptPasswordEncoder();
	    }
	    /** 
	     * allow cross origin requests 
	     * 跨域相关，请百度 
	     */  
	    @Bean  
	    public WebMvcConfigurer corsConfigurer() {  
	        return new WebMvcConfigurerAdapter() {  
	            @Override  
	            public void addCorsMappings(CorsRegistry registry) {  
	                registry  
	                        .addMapping("/**")  
	                        .allowedOrigins("*")  
	                        .allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE")  
	                        .allowedHeaders("*");  
	            }  
	        };  
	    }  
	    @Bean  
	    public MultipartConfigElement multipartConfigElement() {  
	        MultipartConfigFactory factory = new MultipartConfigFactory();  
	        factory.setMaxFileSize("12800KB");  
	        factory.setMaxRequestSize("12800KB");  
	        return factory.createMultipartConfig();  
	    }  
}
